package com.example.zjhuojback.controller;

import com.example.zjhuojback.common.Result;
import com.example.zjhuojback.entity.Forum;
import com.example.zjhuojback.entity.User;
import com.example.zjhuojback.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        try {
            userService.insertUser(user);
        } catch (Exception e) {
            return Result.error("插入数据错误");
        }
        return Result.success();
    }
    /*修改用户信息*/
    @PutMapping ("/update")
    public Result updata(@RequestBody User user) {
        userService.updataUser(user);
        return Result.success();
    }

    /*修改用户权限*/
    @PutMapping ("/updateUserPermission/{username}/{permission}")
    public Result updateUserPermission(@PathVariable String username, @PathVariable Integer permission) {
        userService.updateUserPermission(username, permission);
        return Result.success();
    }

    /*删除用户信息*/
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.deleteUser(id);
        return Result.success();
    }

    /*删除用户信息*/
    @PutMapping("resetPossword/{id}")
    public Result resetPossword(@PathVariable Integer id) {
        userService.resetPossword(id);
        return Result.success();
    }

    /*批量删除 {2,7,8}*/
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        userService.betchDeleteUser(ids);
        return Result.success();
    }

    /*查询全部用户信息*/
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<User> userList = userService.selectAll();
        return Result.success(userList);
    }

    /*根据id查询用户姓名*/
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        User user = userService.selectById(id);
        return Result.success(user.getName());
    }

}
